<!--
 * @Author: krsea 965837746@qq.com
 * @Date: 2025-03-04 16:46:22
 * @LastEditors: krsea 965837746@qq.com
 * @LastEditTime: 2025-03-04 17:32:02
 * @FilePath: \vue3-code\2305\week3\m7子组件向父组件通信emit.html
 * @Description: 
 * 
 * Copyright (c) 2025 by ${git_name_email}, All Rights Reserved. 
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>组件的基本使用</title>
    <!-- 引入 vue.js -->
    <script src="../js/vue.js"></script>
</head>

<body>
    <!-- div id 为 app 挂载点 -->
    <div id="app">
        <h2>总数：{{ total }}</h2>
        <my-btn @increase="getTotal" @reduce="getTotal"></my-btn>
    </div>

    <!-- 定义组件模板 -->
    <template id="temp">
        <div>
            <button v-on:click="add">单击+1</button>
            <button @click="red">单击-1</button>
        </div>
    </template>
    <!-- 使用 javascript 脚本 -->
    <script type="text/javascript">
        Vue.component('my-btn', {
            // ... options ...
            template:'#temp',
            data(){
                return{
                    count:0
                }
            },
            methods:{
                add(){
                    this.count++;
                    console.log('add() 加速'+this.count);
                    this.$emit('increase',this.count);
                },
                red(){
                    this.count--;
                    console.log('red() 减速'+this.count);
                    this.$emit('reduce',this.count);
                }
            }
        })

        var vm = new Vue({
            el: '#app',
            data: {
                total: 0
            },
            methods: {
                getTotal(t){
                    this.total = t;
                    console.log('getTotal() 获取到子组件传递过来的值：'+t);
                }
            }
        })
    </script>
</body>

</html>